package exteditapp.command;

import java.util.ArrayList;

import exteditapp.FileDefault;

//TODO run command in separated thread

/**
 * Interface for standard command
 * @author Arturas Smorgun, arturas@smorgun.lt
 */
public abstract class ICommand implements Runnable {

	/**
	 * File, on which we going to do operation
	 */
	protected FileDefault file = null;

	/**
	 * Given parameters
	 */
	protected ArrayList<String> parameters = null;
	
	/**
	 * Trigger, which shows whether operation in progress now or not
	 */
	protected boolean execution = false;
	
	public ICommand(){	
	}
	
	public ICommand(FileDefault file){
		setProgress(file);
	}
	
	public ICommand(FileDefault file, String parameters){
		setProgress(file);
		setParams(parseParams(parameters));
	}

	/**
	 * Return working file
	 * @return working file
	 */	
	public synchronized FileDefault getProgress(){
		return this.file;
	}	

	/**
	 * Set the value of progress. Cannot be used on execution.
	 * @param file new working file
	 */
	public synchronized boolean setProgress(FileDefault file){
		if(!execution){
			this.file = file;
			return true;
		}
		return false;
	}	
	
	/**
	 * Return parameters
	 * @return current parameters
	 */	
	public synchronized ArrayList<String> getParams(){
		return this.parameters;
	}	

	/**
	 * Set the value of parameter string. Cannot be used on execution.
	 * @param parameters new parameters
	 */
	public synchronized boolean setParams(ArrayList<String> parameters){
		if(!execution){
			this.parameters = parameters;
			return true;
		}
		return false;
	}
	
	/**
	 * Abstraction of execution procedure for all operations
	 * @param params string representation of parameters
	 * @return true on success and false otherwise
	 */
	public abstract Boolean execute();
	
	protected ArrayList<String> parseParams(String paramString){
		ArrayList<String> result = new ArrayList<String>();
		String[] pArr = paramString.split(", ");
		for(int i = 0; i < pArr.length; i++){
			result.add(pArr[i]);
		}
		return result;
	}
}
